module UrlEnhancerHelper
  
  def url_translate_element source_id,target_id,language_from = "zh",language_to = "en"

    <<-EOS
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>

    <script type="text/javascript">

      function slugify(str) {
        return str.toLowerCase().replace(/[^a-z0-9-_]+/g, '-').replace(/^-|-$/g, '');
      }

      google.load("language", "1");

      function translate_url() {
        if($F("#{source_id}").blank()) {
          alert("请先填写标题");
          return;
        }
        $("#{target_id}").value = "正在翻译中...";
        google.language.translate($F("#{source_id}"), "#{language_from}", "#{language_to}", function(result) {
          if (!result.error) {
            $("#{target_id}").value = slugify(result.translation);
          }
        });
      }  
    </script>
    EOS
    
  end
  
end